package leetcode.editor.cn.q0xx

import kotlin.math.max

class Q67 {
    fun addBinary(a: String, b: String): String {
        val buffer = StringBuilder()
        val n = max(a.length, b.length)
        var carry = 0
        for (i in 0 until n) {
            carry += if (i<a.length) a[a.length-i-1] - '0' else 0
            carry += if (i<b.length) b[b.length-i-1] - '0' else 0
            buffer.append('0' + carry % 2)
            carry /= 2
        }
        if (carry>0)
            buffer.append('1')
        return buffer.reverse().toString()
    }
}